package org.wizehack.mri.testSet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.wizehack.mri.exception.InvalidFileExtensionException;
import org.wizehack.mri.io.FileCopy;
import org.wizehack.mri.test.TestSuite;

public abstract class TestCaseReductionExt extends TestCaseReduction implements TestCaseSelectionExt {

	/**
	 * this method returns test suite selected by statement vector based reduction strategy.
	 */
	@Override
	public TestSuite select() {
		// TODO Auto-generated method stub
		return this.getVactorBasedTestSuite();
	}

	private TestSuite getVactorBasedTestSuite() {
		VectorBasedStrategy strategy = new VectorBasedStrategy();
		String dir = super.getDirectory();
		TestSuite tSuite = super.getTestSuite();
		List<String> failedCoverageFileList = super.getFailedCoverageFileList();
		strategy.setDirectory(dir);
		strategy.setTestSuite(tSuite);
		strategy.setFailedCoverageFileList(failedCoverageFileList);
		TestSuite vactorBasedTestSuite = strategy.select();
		return vactorBasedTestSuite;
	}
	
	public void exportTo(List<TestSuite> testSuiteList, String destDir, String dtdFile) throws InvalidFileExtensionException{
		FileCopy fcopy = new FileCopy();
		File dtd = new File(dtdFile);
		
		for(int i=1; i<=testSuiteList.size(); i++){
			if(dtdFile.lastIndexOf(".dtd")>0){
				String destDTD = destDir + File.separator + "test_suite_"+i + File.separator + dtd.getName();
				try {
					fcopy.copy(dtdFile, destDTD);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} else {
				throw new InvalidFileExtensionException(dtdFile);
			}
			
			TestSuite testSuite = testSuiteList.get(i-1);
			
			for(int j=0; j<testSuite.size(); j++){
				String xmlFile = testSuite.get(j).getCoverageLocation();
				
				File file = new File(xmlFile);
				String destFile = destDir + File.separator + "test_suite_"+i + File.separator + file.getName();
				
				try {
					fcopy.copy(xmlFile, destFile);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
